package com.launchrock.projectadvisor.skeleton_BGA;

import android.util.Log;


public class Grid {
	
	private final int ROWS = 37;
	private final int COLUMNS = 30;
	private int tileSize=Tile.getHeight();
	private Tile [][]mGrid;
	private static Grid grid = new Grid();
	
	private Grid ()
	{
		mGrid=new Tile[ROWS][COLUMNS];
		positionTiles();
		loadlevel();
	}
	
	
	
	public static Grid getInstance(){
		return grid;
	}
	
	public Tile getTileFromGrid(int x, int y)
	{
		return mGrid[y][x];
	}
	
	public static void setDimensions(int sHeight, int sWidth){
		Tile.setSize(sHeight, sWidth);
	}
	
	public void positionTiles()
	{
		int xPos=0, yPos=0;
		int dx= Tile.getWidth(), dy = Tile.getHeight();
		for(int i=0; i < ROWS;i++)
		{			
			for(int j =0; j<COLUMNS; j++)
			{
				mGrid[i][j]=new Tile();
				mGrid[i][j].setPosition(xPos, yPos);
				//Log.i("Casilla","["+i+","+j+"]:["+xPos+","+yPos+"]");
				xPos+=dx;								
			}
			xPos=0;
			yPos+=dy;			
		}
	}
	public void loadlevel(){
		int [][]Matriz={{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                		{0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 3, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3, 0, 2, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 3, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 2, 2, 2, 2, 2, 2, 2, 3, 0, 0},
		                {0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 2, 2, 2, 2, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 2, 2, 3, 2, 2, 2, 3, 0, 2, 0, 0, 2, 0, 2, 0, 2, 0, 0, 0, 0, 3, 2, 2, 2, 3, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 2, 3, 0, 0, 2, 0, 2, 2, 4, 2, 2, 2, 2, 2, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 2, 2, 2, 2, 3, 3, 2, 3, 2, 3, 2, 4, 2, 2, 2, 2, 2, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 3, 2, 2, 2, 0, 2, 0, 0, 0, 2, 0, 3, 2, 2, 2, 2, 3, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 3, 2, 2, 3, 0, 2, 0, 0, 0, 2, 0, 2, 3, 2, 2, 2, 2, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 3, 2, 2, 3, 2, 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0},
		                {0, 0, 3, 2, 2, 4, 2, 3, 0, 0, 2, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 3, 2, 3, 2, 3, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 2, 2, 2, 3, 2, 2, 2, 0, 0, 0, 2, 2, 3, 2, 2, 2, 2, 0, 2, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0, 0, 2, 0, 2, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0},
		                {0, 0, 3, 2, 2, 3, 2, 3, 2, 2, 2, 4, 2, 4, 2, 2, 2, 4, 2, 4, 2, 2, 2, 2, 2, 2, 2, 3, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0},
		                {0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 2, 0, 2, 0, 0},
		                {0, 0, 2, 2, 2, 3, 2, 3, 2, 2, 2, 3, 2, 3, 2, 2, 2, 3, 2, 3, 2, 2, 2, 3, 2, 3, 2, 2, 0, 0},
		                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		                {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
				
		for(int i=2; i < ROWS-2;i++)
		{			
			for(int j =2; j<COLUMNS-2; j++)
			{
				if(Matriz[i][j]!=0){
					mGrid[i][j].setExits(Matriz[i][j-1],Matriz[i-1][j],Matriz[i][j+1],Matriz[i+1][j]);
				}
				else{
					mGrid[i][j].setExits(0, 0, 0, 0);
				}										
			}			
		}
		
	}	
	
	public boolean isBlocked(int x, int y, Direction nextTile)
	{
		switch(nextTile)
		{
		case left:
			if(mGrid[y][x].getLExit()>0)
				return false;
			return true;
		case up:
			if(mGrid[y][x].getUExit()>0)
				return false;
			return true;
		case right:
			if(mGrid[y][x].getRExit()>0)
				return false;
			return true;
		case down:
			if(mGrid[y][x].getDExit()>0)
				return false;
			return true;
		default:
			return true;			
		}		
	}
}
